#[===[.md:
# Just like https://github.com/microsoft/vcpkg/blob/master/scripts/cmake/vcpkg_common_definitions.cmake but add some additional system libraries.

This file defines the following variables which are commonly needed or used in portfiles:

```cmake
ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_<target>                 with <target> being one of the following: WINDOWS, UWP, LINUX, OSX, ANDROID, FREEBSD, OPENBSD. only defined if <target>
ATFRAMEWORK_CMAKE_TOOLSET_HOST_IS_<target>                   with <host> being one of the following: WINDOWS, LINUX, OSX, FREEBSD, OPENBSD. only defined if <host>
ATFRAMEWORK_CMAKE_TOOLSET_HOST_PATH_SEPARATOR                Host specific path separator (USAGE: "<something>${ATFRAMEWORK_CMAKE_TOOLSET_HOST_PATH_SEPARATOR}<something>"; only use and pass variables with ATFRAMEWORK_CMAKE_TOOLSET_HOST_PATH_SEPARATOR within "")
ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES                   list of libraries are provide by the toolchain and are not managed by vcpkg
```

CMAKE_STATIC_LIBRARY_(PREFIX|SUFFIX), CMAKE_SHARED_LIBRARY_(PREFIX|SUFFIX) and CMAKE_IMPORT_LIBRARY_(PREFIX|SUFFIX) are defined for the target
Furthermore the variables CMAKE_FIND_LIBRARY_(PREFIXES|SUFFIXES) are also defined for the target so that
portfiles are able to use find_library calls to discover dependent libraries within the current triplet for ports.
#]===]

include_guard(GLOBAL)

# Helper variable to identify the Target system. ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_<targetname>.

# Crossing compiling must be check first
if(ANDROID OR CMAKE_SYSTEM_NAME STREQUAL "Android")
  set(ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_ANDROID ON)
elseif(CMAKE_SYSTEM_NAME STREQUAL "iOS")
  set(ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_IOS ON)
elseif(DEFINED VCPKG_CMAKE_SYSTEM_NAME AND VCPKG_CMAKE_SYSTEM_NAME STREQUAL "WindowsStore")
  set(ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_WINDOWS ON)
  set(ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_UWP ON)
elseif(MINGW OR CMAKE_SYSTEM_NAME STREQUAL "MinGW")
  set(ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_WINDOWS ON)
  set(ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_MINGW ON)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
  set(ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_WINDOWS ON)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  set(ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_OSX ON)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  set(ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_LINUX ON)
elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
  set(ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_FREEBSD ON)
elseif(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
  set(ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_OPENBSD ON)
endif()

# Helper variables to identify the host system name
if(CMAKE_HOST_WIN32)
  set(ATFRAMEWORK_CMAKE_TOOLSET_HOST_IS_WINDOWS ON)
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
  set(ATFRAMEWORK_CMAKE_TOOLSET_HOST_IS_OSX ON)
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
  set(ATFRAMEWORK_CMAKE_TOOLSET_HOST_IS_LINUX ON)
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "FreeBSD")
  set(ATFRAMEWORK_CMAKE_TOOLSET_HOST_IS_FREEBSD ON)
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "OpenBSD")
  set(ATFRAMEWORK_CMAKE_TOOLSET_HOST_IS_OPENBSD ON)
endif()

# Helper variable to identify the host path separator.
if(CMAKE_HOST_WIN32)
  set(ATFRAMEWORK_CMAKE_TOOLSET_HOST_PATH_SEPARATOR ";")
elseif(CMAKE_HOST_UNIX)
  set(ATFRAMEWORK_CMAKE_TOOLSET_HOST_PATH_SEPARATOR ":")
endif()

# Append platform libraries to ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES The variable are just appended to permit to
# custom triplets define the variable

if(ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_ANDROID)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES "log" "c")
endif()

if(ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_LINUX
   OR ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_FREEBSD
   OR ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_OPENBSD
   OR ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_OSX
   OR ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_MINGW
   OR ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_IOS
   OR ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_ANDROID)
  set(ATFRAMEWORK_CMAKE_TOOLSET_TEST_BACKUP_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  set(ATFRAMEWORK_CMAKE_TOOLSET_TEST_SYSTEM_LIBRARIES m dl pthread rt gcc gcc_s)
  include(CheckCXXSourceCompiles)

  foreach(LIBNAME IN LISTS ATFRAMEWORK_CMAKE_TOOLSET_TEST_SYSTEM_LIBRARIES)
    if(ATFRAMEWORK_CMAKE_TOOLSET_TEST_BACKUP_CMAKE_REQUIRED_LIBRARIES)
      set(CMAKE_REQUIRED_LIBRARIES "${ATFRAMEWORK_CMAKE_TOOLSET_TEST_BACKUP_CMAKE_REQUIRED_LIBRARIES};${LIBNAME}")
    else()
      set(CMAKE_REQUIRED_LIBRARIES "${LIBNAME}")
    endif()
    string(TOUPPER "${LIBNAME}" ATFRAMEWORK_CMAKE_TOOLSET_TEST_NAME)
    set(ATFRAMEWORK_CMAKE_TOOLSET_TEST_NAME
        "ATFRAMEWORK_CMAKE_TOOLSET_TEST_LINK_${ATFRAMEWORK_CMAKE_TOOLSET_TEST_NAME}")
    check_cxx_source_compiles("#include <cstdio>
    int main() { return 0; }" ${ATFRAMEWORK_CMAKE_TOOLSET_TEST_NAME})
    if(${ATFRAMEWORK_CMAKE_TOOLSET_TEST_NAME})
      list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES "${LIBNAME}")
    endif()
  endforeach()

  unset(ATFRAMEWORK_CMAKE_TOOLSET_TEST_NAME)
  if(ATFRAMEWORK_CMAKE_TOOLSET_TEST_BACKUP_CMAKE_REQUIRED_LIBRARIES)
    set(CMAKE_REQUIRED_LIBRARIES "${ATFRAMEWORK_CMAKE_TOOLSET_TEST_BACKUP_CMAKE_REQUIRED_LIBRARIES}")
  else()
    unset(CMAKE_REQUIRED_LIBRARIES)
  endif()
endif()

# Platforms with system iconv
if(ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_OSX OR ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_IOS)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES iconv)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES resolv)
endif()

# Windows system libs
if(ATFRAMEWORK_CMAKE_TOOLSET_TARGET_IS_WINDOWS)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES advapi32)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES bcrypt)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES dinput8)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES gdi32)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES imm32)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES oleaut32)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES ole32)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES iphlpapi)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES psapi)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES secur32)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES setupapi)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES shell32)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES shlwapi)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES strmiids)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES user32)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES uuid)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES version)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES vfw32)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES winmm)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES wsock32)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES userenv)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES Ws2_32)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES wldap32)
  list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES crypt32)
endif()

if(MSVC)
  foreach(LIBNAME IN LISTS ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES)
    list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LINKS "${LIBNAME}.lib")
  endforeach()
else()
  foreach(LIBNAME IN LISTS ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LIBRARIES)
    list(APPEND ATFRAMEWORK_CMAKE_TOOLSET_SYSTEM_LINKS "-l${LIBNAME}")
  endforeach()
endif()
